package com.xzssmk.leisure.x.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzssmk.leisure.x.mapper.PeriodLargessMapper;
import com.xzssmk.leisure.x.po.PeriodLargess;
import com.xzssmk.leisure.x.po.PeriodLargessExt;
import com.xzssmk.leisure.x.service.BaseService;
import com.xzssmk.leisure.x.service.PeriodLargessService;
import com.xzssmk.leisure.x.util.DataTables;
import com.xzssmk.leisure.x.util.RequestStatusUtil;

@Service
public class PeriodLargessServiceImpl extends BaseService implements PeriodLargessService {

    @Autowired
    private PeriodLargessMapper periodLargessMapper;

    @Override
    public Map<String, Object> add(PeriodLargess periodLargess) {

        Map<String, Object> map = new HashMap<>();
        if (0 != periodLargessMapper.selectRecordCount(periodLargess.getIssuePeriodId(),
            periodLargess.getStoreLargessId(), periodLargess.getPriorityLevel())) {
            map.put("status", RequestStatusUtil.FAILURE);
            return map;
        }

        Integer id = periodLargessMapper.insert(periodLargess);
        if (id > 0) {
            map.put("status", RequestStatusUtil.SUCCESS);
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
        }
        return map;
    }

    @Override
    public Map<String, Object> delete(List<Integer> idlist) {
        Map<String, Object> map = new HashMap<>();
        if (periodLargessMapper.deleteByPrimaryKeys(idlist) > 0) {
            map.put("status", RequestStatusUtil.SUCCESS);
        } else {
            map.put("status", RequestStatusUtil.FAILURE);
        }
        return map;
    }

    @Override
    public DataTables getPageList(DataTables dataTables, String issuePeriodId) {
        PageHelper.startPage(dataTables.getStart(), dataTables.getLength()); // 核心分页代码
        PageHelper.orderBy("modify_time desc");
        // https: // blog.csdn.net/lvbang_lzt/article/details/54375091
        // https: // blog.csdn.net/evane1890/article/details/63262610
        // https: // blog.csdn.net/qq_33315102/article/details/80572566
        if (!StringUtils.isEmpty(dataTables.getColumn())) {
            String column = propertyToColumn(PeriodLargessMapper.class, dataTables.getColumn());
            if (!StringUtils.isEmpty(column)) {
                PageHelper.orderBy(column + " " + dataTables.getOrder());
            }
        }
        List<PeriodLargessExt> list = periodLargessMapper.getPageList(issuePeriodId);
        PageInfo<PeriodLargessExt> pageInfo = new PageInfo<PeriodLargessExt>(list);
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<>());

        return dataTables;
    }
}
